package client;

import java.applet.Applet;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

import javax.imageio.ImageIO;

public class Lot {

	private int lotID;
	private int lotCount;
	private int lotSize;
	private URL url;
	private BufferedImage lotImageGrey;
	private BufferedImage lotImageGreen;
	private BufferedImage lotImageYellow;
	private BufferedImage lotImageRed;
	public int x;
	public int y;
	private Rectangle bounds;
	private URL codeBase;
	
	public Lot (int lotID, int x, int y, URL cb) throws IOException{
		String fileName = "imgs\\lot"+lotID+"Grey.png";
		this.lotID = lotID;
		this.codeBase = cb;
		this.url = new URL(cb, fileName);
		this.lotImageGrey = ImageIO.read(url); 
		this.x = x;
		this.y = y;
		bounds = new Rectangle(x,y,lotImageGrey.getWidth(), lotImageGrey.getHeight());
		loadLotImages();
	}
	
	public void loadLotImages() throws IOException{
		lotImageGreen = ImageIO.read(new URL(codeBase,"imgs\\lot"+lotID+"Green.png"));
		lotImageYellow = ImageIO.read(new URL(codeBase,"imgs\\lot"+lotID+"Yellow.png"));
		lotImageRed = ImageIO.read(new URL(codeBase,"imgs\\lot"+lotID+"Red.png"));
	}
	
	public Image getLotImage(){
		int spaces = lotSize - lotCount;
		if (spaces <= 1)
			return lotImageRed;
		if (spaces <= 5)
			return lotImageYellow;
		if (spaces > 5)
			return lotImageGreen;
		return lotImageGrey;
	}
	
	public Image randomLotImage(){
		int x = new Random().nextInt(4);
		switch(x){
		case 0:
			return lotImageGreen;
		case 1:
			return lotImageYellow;
		case 2: 
			return lotImageRed;
		}
		return lotImageGrey;
	}

	public boolean isClicked(Point p) {
		if (bounds.contains(p))
			return true;
		return false;
	}

	public void setLotID(int lotID) {
		this.lotID = lotID;
	}
	
	public void setLotCount(int lotCount) {
		this.lotCount = lotCount;
	}
	
	public void setLotSize(int lotSize) {
		this.lotSize = lotSize;
	}

	public int getLotID() {
		return this.lotID;
	}
	
	public int getLotCount() {
		return this.lotCount;
	}
	
	public int getLotSize() {
		return this.lotSize;
	}
	
}
